# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "../include/Hashing.h"

/*typedef struct FileDescriptor {
	char fileName[50];
	char filePath[100];
	char fileType[5];
	int size;
	//Location Block Number
}fd;*/

struct HashNode {
	struct FileDescriptor *fd_node;
	struct HashNode *next;
	struct HashNode *previous;
};

struct HashNode *addHashNode (struct FileDescriptor *node, struct HashNode *header) {
	struct HashNode *temp;
	temp = (struct HashNode*) malloc (sizeof(struct HashNode));
	temp -> fd_node = node;
	temp -> previous = NULL;
	temp -> next = NULL;
	if (header == NULL)
		header = temp;
	else {
		temp -> next = header;
		header -> previous = temp;
		header =  temp;
	}
	return header;
}

void displayList (struct HashNode *header) {
	struct HashNode *temp;
	temp = header;
	printf("\nFile Name\tFile Path\n");
	while (temp) {
		printf("%s\t%s\n", temp -> fd_node -> fileName, temp -> fd_node -> filePath);
		temp = temp -> next;
	}
}

struct HashNode *deleteNode (struct FileDescriptor *node, struct HashNode *header) {
	struct HashNode *temp;
	temp = header;
	if (temp -> fd_node == node)
		header = temp -> next;
	while (temp) {
		if (temp -> fd_node == node){
			if (temp -> next)
				(temp -> next) -> previous = temp -> previous;
			if (temp -> previous)
				(temp -> previous) -> next = temp -> next;
			free (temp);
		}
		temp = temp -> next;
	}
	return header;
}

struct HashNode **hash (char *fn, struct HashNode *table[]) {
	struct HashNode ** temp;
	int key;
	char ch = *fn;
	if (ch >= 65 && ch <= 90)
		key = ch - 65;
	else if (ch >= 97 && ch <= 122)
		key = ch - 97;
	temp = &table[key];
	return temp;
}

struct HashNode *search (char *fn, struct HashNode *table[]) {
	struct HashNode **temp, *travs;
	temp = hash (fn, table);
	travs = *temp;
	while (travs) {
		if (!strcmp(travs -> fd_node -> fileName, fn))
		{
			printf("%s\n%s\n",travs -> fd_node -> fileName, fn);
			return travs;
		}
		travs = travs -> next;
	}
	return NULL;
}

void displayHashTable (struct HashNode *table[]) {
	int i;
	for (i = 0; i < 26; i++) {
		printf ("\nHash [%d] : ", i);
		displayList (table[i]);
	}
}
/*
int main (){
	struct HashNode *table [26], **temp, *item;
	int i;
	struct FileDescriptor * files[10];
	for (i = 0; i < 26; i++) {
		table[i] = NULL;
	}
	files[0] = (struct FileDescriptor *) malloc (sizeof(struct FileDescriptor));
	strcpy (files[0] -> fileName, "aaa");
	strcpy (files[0] -> filePath, "/home/aaa1/");
	strcpy (files[0] -> fileType, ".txt");
	files[1] = (struct FileDescriptor *) malloc (sizeof(struct FileDescriptor));
	strcpy (files[1] -> fileName, "bbb");
	strcpy (files[1] -> filePath, "/home/aaa2/");
	strcpy (files[1] -> fileType, ".txt");
	files[2] = (struct FileDescriptor *) malloc (sizeof(struct FileDescriptor));
	strcpy (files[2] -> fileName, "abb");
	strcpy (files[2] -> filePath, "/home/aaa3/");
	strcpy (files[2] -> fileType, ".txt");
	files[3] = (struct FileDescriptor *) malloc (sizeof(struct FileDescriptor));
	strcpy (files[3] -> fileName, "bac");
	strcpy (files[3] -> filePath, "/home/aaa4/");
	strcpy (files[3] -> fileType, ".txt");	
	files[4] = (struct FileDescriptor *) malloc (sizeof(struct FileDescriptor));
	strcpy (files[4] -> fileName, "cab");
	strcpy (files[4] -> filePath, "/home/bbb/");
	strcpy (files[4] -> fileType, ".txt");
	files[5] = (struct FileDescriptor *) malloc (sizeof(struct FileDescriptor));
	strcpy (files[5] -> fileName, "dac");
	strcpy (files[5] -> filePath, "/home/aaa5/");
	strcpy (files[5] -> fileType, ".txt");
	files[6] = (struct FileDescriptor *) malloc (sizeof(struct FileDescriptor));
	strcpy (files[6] -> fileName, "dca");
	strcpy (files[6] -> filePath, "/home/aaa6/");
	strcpy (files[6] -> fileType, ".txt");
	files[7] = (struct FileDescriptor *) malloc (sizeof(struct FileDescriptor));
	strcpy (files[7] -> fileName, "evc");
	strcpy (files[7] -> filePath, "/home/aaa7/");
	strcpy (files[7] -> fileType, ".txt");
	files[8] = (struct FileDescriptor *) malloc (sizeof(struct FileDescriptor));
	strcpy (files[8] -> fileName, "aaf");
	strcpy (files[8] -> filePath, "/home/aaa8/");
	strcpy (files[8] -> fileType, ".txt");
	files[9] = (struct FileDescriptor *) malloc (sizeof(struct FileDescriptor));
	strcpy (files[9] -> fileName, "xys");
	strcpy (files[9] -> filePath, "/home/aaa9/");
	strcpy (files[9] -> fileType, ".txt");
	
	for (i = 0; i < 10; i++) {
		temp = hash (files[i]->fileName, table);
		*temp = addHashNode (files[i], *temp);
	}
	for (i = 0; i < 26; i++) {
		printf ("\nHash [%d] : ", i);
		displayList (table[i]);
	}
	
	item = search ("dca", table);
	if (item)
		printf ("File dca found in location %s\n", item -> fd_node -> filePath);
	else
		printf ("File dca not found\n");
	
	item = search ("dcdafsa", table);
	if (item)
		printf ("File dcdafsa found in location %s\n", item -> fd_node -> filePath);
	else
		printf ("File dcdafsa not found\n");
		
	return 0;
}*/
